package com.hzya.frame.system.dictionaryshop.controller;

import com.hzya.frame.system.dictionaryshop.entity.DictionaryShopEntity;
import com.hzya.frame.system.dictionaryshop.service.IDictionaryShopService;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.core.page.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description: 数据字典  controller
 * @tableName: system_dictionaryshop
 * @entityName: DictionaryShopEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history: 1.0
 */

@Controller("system_dictionaryshop")
@RequestMapping("/system/dictionaryshop/DictionaryShopController")
public class DictionaryShopController extends DefaultController {
    @Autowired
    protected IDictionaryShopService dictionaryshopService;

    /**
     * 数据字典分页信息
     **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageInfo<DictionaryShopEntity> queryPaged(DictionaryShopEntity entity) throws Exception {
        PageInfo<DictionaryShopEntity> page = null;
        try {
            page = dictionaryshopService.queryPaged(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 跳转到数据字典的index页面
     **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        //view.addObject(attributeName, attributeValue);
        List<DictionaryShopEntity> typeList = dictionaryshopService.getTypeList();
        view.addObject("typeList", typeList);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/dictionaryshop/dictionaryshop/list");
        return view;
    }

    /**
     * 跳转到数据字典信息新增页面
     **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage() {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        //view.addObject(attributeName, attributeValue);

        /**这里是返回对应的页面路径**/
        view.setViewName("/system/dictionaryshop/dictionaryshop/add");
        return view;
    }

    /**
     * 数据字典新增信息保存
     **/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save(DictionaryShopEntity entity) {
        try {
            dictionaryshopService.saveMulit(entity);
            return getSuccessMessageEntity("保存成功", "save");
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "save");
        }
    }

    /**
     * 跳转到数据字典信息修改页面
     **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        DictionaryShopEntity entity = dictionaryshopService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/dictionaryshop/dictionaryshop/edit");
        return view;
    }

    /**
     * 数据字典修改信息保存
     **/
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update(DictionaryShopEntity entity) {
        try {
            dictionaryshopService.update(entity);
            return getSuccessMessageEntity("更新成功！", "update");
        } catch (Exception e) {
            logger.error("update", e);
            e.printStackTrace();
            return getFailureMessageEntity("更新失败！" + e.getMessage(), "update");
        }
    }

    /**
     * 跳转到数据字典信息查看
     **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        DictionaryShopEntity entity = dictionaryshopService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/dictionaryshop/dictionaryshop/show");
        return view;
    }

    /**
     * 删除数据字典信息
     **/
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
        try {
            dictionaryshopService.logicRemove(id);
            return getSuccessMessageEntity("删除成功！", "delete");
        } catch (Exception e) {
            logger.error("delete", e);
            e.printStackTrace();
            return getFailureMessageEntity("删除失败！" + e.getMessage(), "delete");
        }
    }

    @RequestMapping(value = "batchSaveExp", method = RequestMethod.POST)
    public String batchSaveExp(@ModelAttribute DictionaryShopEntity dictionaryShopEntity) {
        String result = null;
        try {
            dictionaryshopService.batchSave(dictionaryShopEntity.getSysconfigList());
            result = "redirect:/system/dictionaryplatform/sysconfig?tab_name=system";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "sysconfig", method = RequestMethod.GET)
    public ModelAndView sysparamlist(@ModelAttribute DictionaryShopEntity entity) {
        ModelAndView result = new ModelAndView();
        if (null == entity) {
            entity = new DictionaryShopEntity();
            entity.setTab_name("system");
        }
        try {
            List<DictionaryShopEntity> tempList = dictionaryshopService.query(entity);
            result.setViewName("/system/dictionaryplatform/sysconfiglist");
            result.addObject("sysconfList", tempList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

